package com.blog.service.impl;


 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.blog.constants.SystemConstants;
 import com.blog.domain.DTO.AddCategoryDto;
 import com.blog.domain.DTO.CategoryListDto;
 import com.blog.domain.ResponseResult;
 import com.blog.domain.entity.Article;
 import com.blog.domain.entity.Category;
 import com.blog.domain.vo.CategoryVo;
 import com.blog.domain.vo.PageVo;
 import com.blog.mapper.CategoryMapper;
 import com.blog.service.ArticleService;
 import com.blog.service.CategoryService;
 import com.blog.utils.BeanCopyUtils;
 import com.blog.utils.SecurityUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.util.StringUtils;

 import java.util.List;
 import java.util.Set;
 import java.util.stream.Collectors;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2024-08-25 14:03:17
 */
 @Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

   @Autowired
    private ArticleService articleService;
    @Override
    public ResponseResult getCategoryList() {
     //查询文章表，状态为已发布的文章
     LambdaQueryWrapper<Article> articleLambdaQueryWrapper = new LambdaQueryWrapper<>();
     //查询条件
     articleLambdaQueryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
     List<Article> articleList = articleService.list(articleLambdaQueryWrapper);
     //获取文章的分类id，去重保存
     Set<Long> categoryIds = articleList.stream()
             .map(article -> article.getCategoryId())
             .collect(Collectors.toSet());
     //根据分类id查询分类表得到分类
     List<Category> categories = listByIds(categoryIds);
     categories.stream().filter(category -> SystemConstants.STATUS_NORMAL.equals(category.getStatus()));
     //封装vo返回
     List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categories, CategoryVo.class);
     return ResponseResult.okResult(categoryVos);
    }

	@Override
	public ResponseResult<CategoryVo> selectAllCategory() {
     LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
     wrapper.eq(Category::getStatus,"0");
     List<Category> list = list(wrapper);
     List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(list, CategoryVo.class);
     return ResponseResult.okResult(categoryVos);
	}

 /**分类分页查询
  *
  * @param pageNum
  * @param pageSize
  * @param categoryListDto
  * @return
  */

 @Override
 public ResponseResult categoryList(Integer pageNum, Integer pageSize,
                                       CategoryListDto categoryListDto) {

  //分页条件
  Page<Category> page = new Page<>();
  page.setCurrent(pageNum);
  page.setSize(pageSize);
  //查询条件
  LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
  wrapper.eq(StringUtils.hasText(categoryListDto.getStatus()),
          Category::getStatus,categoryListDto.getStatus());

  wrapper.like(StringUtils.hasText(categoryListDto.getName()),
          Category::getName,categoryListDto.getName());

  //分页

  page(page,wrapper);
  //封装数据返回
  long total = page.getTotal();

  List<Category> records = page.getRecords();

  List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(records, CategoryVo.class);


  PageVo pageVo = PageVo.builder()
          .total(total)
          .rows(categoryVos)
          .build();

  return ResponseResult.okResult(pageVo);
	}

 /**新增分类
  *
  * @param addCategoryDto
  * @return
  */
 @Override
 public ResponseResult addCategory(AddCategoryDto addCategoryDto) {
  Category category = BeanCopyUtils.copyBean(addCategoryDto, Category.class);
  save(category);
  return ResponseResult.okResult();

 }

 /**根据id查询分类回显
  *
  * @param id
  * @return
  */

 @Override
 public ResponseResult getCategoryById(Long id) {
  Category category = getById(id);
  CategoryVo categoryVo = BeanCopyUtils.copyBean(category, CategoryVo.class);
  return ResponseResult.okResult(categoryVo);
 }

 /**更新分类信息
  *
  * @param categoryVo
  * @return
  */
 @Override
 public ResponseResult updateCategory(CategoryVo categoryVo) {
  Category category = BeanCopyUtils.copyBean(categoryVo, Category.class);
  category.setUpdateBy(SecurityUtils.getUserId());
  updateById(category);
  return ResponseResult.okResult();
 }

 /**删除分类
  *
  * @param ids
  * @return
  */
 @Override
 public ResponseResult deletecategory(List<Long> ids) {
  removeByIds(ids);
  return ResponseResult.okResult();
 }



}
